//
//  LYSettingChanagePasswordController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/24.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYSettingChanagePasswordController.h"
#import "NSString+AES.h"
#import "AppDelegate.h"

@interface LYSettingChanagePasswordController ()

@property (nonatomic,strong) UITextField *oldpasswordTF;
@property (nonatomic,strong) UITextField *newpasswordTF;
@property (nonatomic,strong) UITextField *surepasswordTF;

@property (nonatomic,strong) UIButton *sureBtn;

@end

@implementation LYSettingChanagePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"修改密码";
    
    [self createSubview];
}

- (void)createSubview{
    if (self.isExistPWD) { // 有密码
        UILabel *oldLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 70, 45)];
        oldLabel.text = @"旧密码";
        oldLabel.font = Font(Font_14);
        oldLabel.textColor = THEMECOLOR_OF_TEXT_333333;
        [self.view addSubview:oldLabel];
        
        self.oldpasswordTF = [[UITextField alloc] initWithFrame:CGRectMake(oldLabel.right + 5, oldLabel.top, WIDTH - oldLabel.right - 15, 45)];
        self.oldpasswordTF.placeholder = @"请输入旧密码";
        self.oldpasswordTF.secureTextEntry = YES;
        self.oldpasswordTF.textColor = THEMECOLOR_OF_TEXT_333333;
        self.oldpasswordTF.font = Font(Font_14);
        [self.view addSubview:self.oldpasswordTF];
        
        UIView *oldLine = [[UIView alloc] initWithFrame:CGRectMake(0, oldLabel.bottom, WIDTH, lineH)];
        oldLine.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        [self.view addSubview:oldLine];
        
        UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, oldLine.bottom, 70, 45)];
        newLabel.text = @"新密码";
        newLabel.font = Font(Font_14);
        newLabel.textColor = THEMECOLOR_OF_TEXT_333333;
        [self.view addSubview:newLabel];
        
        self.newpasswordTF = [[UITextField alloc] initWithFrame:CGRectMake(newLabel.right + 5, newLabel.top, WIDTH - newLabel.right - 15, 45)];
        self.newpasswordTF.placeholder = PasswordSetPlaceholderText;
        self.newpasswordTF.secureTextEntry = YES;
        self.newpasswordTF.textColor = THEMECOLOR_OF_TEXT_333333;
        self.newpasswordTF.font = Font(Font_14);
        [self.view addSubview:self.newpasswordTF];
        
        UIView *newLine = [[UIView alloc] initWithFrame:CGRectMake(0, newLabel.bottom, WIDTH, lineH)];
        newLine.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        [self.view addSubview:newLine];
        
        UILabel *sureLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, newLine.bottom, 70, 45)];
        sureLabel.text = @"确认密码";
        sureLabel.font = Font(Font_14);
        sureLabel.textColor = THEMECOLOR_OF_TEXT_333333;
        [self.view addSubview:sureLabel];
        
        self.surepasswordTF = [[UITextField alloc] initWithFrame:CGRectMake(oldLabel.right + 5, sureLabel.top, WIDTH - oldLabel.right - 15, 45)];
        self.surepasswordTF.placeholder = @"请再次输入新密码";
        self.surepasswordTF.secureTextEntry = YES;
        self.surepasswordTF.textColor = THEMECOLOR_OF_TEXT_333333;
        self.surepasswordTF.font = Font(Font_14);
        [self.view addSubview:self.surepasswordTF];
        
        UIView *sureLine = [[UIView alloc] initWithFrame:CGRectMake(0, sureLabel.bottom, WIDTH, lineH)];
        sureLine.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        [self.view addSubview:sureLine];
        
        UIButton *surebutton = [UIButton buttonWithType:UIButtonTypeCustom];
        surebutton.frame = CGRectMake( 40, sureLine.bottom + 50, WIDTH-80, 40);
        self.sureBtn = surebutton;
        surebutton.layer.cornerRadius = surebutton.height/2;
        surebutton.clipsToBounds = YES;
        surebutton.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        surebutton.userInteractionEnabled = NO;
        [surebutton setTitle:@"确定" forState:UIControlStateNormal];
        [surebutton setTitleColor:COLOR_OF_WHITE forState:UIControlStateNormal];
        surebutton.titleLabel.font = Font(Font_14);
        [surebutton addTarget:self action:@selector(surebuttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:surebutton];
    }else{ //无密码
        UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 70, 45)];
        newLabel.text = @"新密码";
        newLabel.font = Font(Font_14);
        newLabel.textColor = THEMECOLOR_OF_TEXT_333333;
        [self.view addSubview:newLabel];
        
        self.newpasswordTF = [[UITextField alloc] initWithFrame:CGRectMake(newLabel.right + 5, newLabel.top, WIDTH - newLabel.right - 15, 45)];
        self.newpasswordTF.placeholder = PasswordSetPlaceholderText;
        self.newpasswordTF.secureTextEntry = YES;
        self.newpasswordTF.textColor = THEMECOLOR_OF_TEXT_333333;
        self.newpasswordTF.font = Font(Font_14);
        [self.view addSubview:self.newpasswordTF];
        
        UIView *newLine = [[UIView alloc] initWithFrame:CGRectMake(0, newLabel.bottom, WIDTH, 0.5)];
        newLine.backgroundColor = THEMECOLOR_OF_GRAY;
        [self.view addSubview:newLine];
        
        UILabel *sureLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, newLine.bottom, 70, 45)];
        sureLabel.text = @"确认密码";
        sureLabel.font = Font(Font_14);
        sureLabel.textColor = THEMECOLOR_OF_TEXT_333333;
        [self.view addSubview:sureLabel];
        
        self.surepasswordTF = [[UITextField alloc] initWithFrame:CGRectMake(sureLabel.right + 5, sureLabel.top, WIDTH - sureLabel.right - 15, 45)];
        self.surepasswordTF.placeholder = @"请再次输入新密码";
        _surepasswordTF.secureTextEntry = YES;
        self.surepasswordTF.textColor = THEMECOLOR_OF_TEXT_333333;
        self.surepasswordTF.font = Font(Font_14);
        [self.view addSubview:self.surepasswordTF];
        
        UIView *sureLine = [[UIView alloc] initWithFrame:CGRectMake(0, sureLabel.bottom, WIDTH, 0.5)];
        sureLine.backgroundColor = THEMECOLOR_OF_GRAY;
        [self.view addSubview:sureLine];
        
        UIButton *surebutton = [UIButton buttonWithType:UIButtonTypeCustom];
        surebutton.frame = CGRectMake( 40, sureLine.bottom + 50, WIDTH-80, 40);
        self.sureBtn = surebutton;
        surebutton.layer.cornerRadius = surebutton.height/2;
        surebutton.clipsToBounds = YES;
//        surebutton.backgroundColor = [UIColor orangeColor];
        surebutton.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        surebutton.userInteractionEnabled = NO;
        [surebutton setTitle:@"确定" forState:UIControlStateNormal];
        [surebutton setTitleColor:COLOR_OF_WHITE forState:UIControlStateNormal];
        surebutton.titleLabel.font = Font(Font_14);
        [surebutton addTarget:self action:@selector(surebuttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:surebutton];
    }
    
    //检测输入框的值变化
    [self textFileChange];
}
- (void)textFileChange{
    [_oldpasswordTF addTarget:self action:@selector(oldpasswordChange:) forControlEvents:UIControlEventEditingChanged];
    [_newpasswordTF addTarget:self action:@selector(passwordNewChange:) forControlEvents:UIControlEventEditingChanged];
    [_surepasswordTF addTarget:self action:@selector(surePasswordChange:) forControlEvents:UIControlEventEditingChanged];
}
- (void)oldpasswordChange:(UITextField *)textFiled {
    _oldpasswordTF.text = textFiled.text;
    NSString *newPWD = _newpasswordTF.text;
    NSString *surePWD = _surepasswordTF.text;
    if (self.isExistPWD) {
        NSString *oldPWD = _oldpasswordTF.text;
        if ((oldPWD.length >= 6 && oldPWD.length <= 20) && (newPWD.length >= 6 && newPWD.length <= 20) && (surePWD.length >= 6 && surePWD.length <= 20)) {
            _sureBtn.userInteractionEnabled = YES;
            _sureBtn.backgroundColor = [UIColor orangeColor];
        }else{
            _sureBtn.userInteractionEnabled = NO;
            _sureBtn.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        }
    }else{
        if ((newPWD.length >= 6 && newPWD.length <= 20) && (surePWD.length >= 6 && surePWD.length <= 20)) {
            _sureBtn.userInteractionEnabled = YES;
            _sureBtn.backgroundColor = [UIColor orangeColor];
        }else{
            _sureBtn.userInteractionEnabled = NO;
            _sureBtn.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        }
    }
}

- (void)passwordNewChange:(UITextField *)textFiled{
    _newpasswordTF.text = textFiled.text;
    NSString *newPWD = _newpasswordTF.text;
    NSString *surePWD = _surepasswordTF.text;
    if (self.isExistPWD) {
        NSString *oldPWD = _oldpasswordTF.text;
        if ((oldPWD.length >= 6 && oldPWD.length <= 20) && (newPWD.length >= 6 && newPWD.length <= 20) && (surePWD.length >= 6 && surePWD.length <= 20)) {
            _sureBtn.userInteractionEnabled = YES;
            _sureBtn.backgroundColor = [UIColor orangeColor];
        }else{
            _sureBtn.userInteractionEnabled = NO;
            _sureBtn.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        }
    }else{
        if ((newPWD.length >= 6 && newPWD.length <= 20) && (surePWD.length >= 6 && surePWD.length <= 20)) {
            _sureBtn.userInteractionEnabled = YES;
            _sureBtn.backgroundColor = [UIColor orangeColor];
        }else{
            _sureBtn.userInteractionEnabled = NO;
            _sureBtn.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        }
    }
}

- (void)surePasswordChange:(UITextField *)textFiled{
    _surepasswordTF.text = textFiled.text;
    NSString *newPWD = _newpasswordTF.text;
    NSString *surePWD = _surepasswordTF.text;
    if (self.isExistPWD) {
        NSString *oldPWD = _oldpasswordTF.text;
        if ((oldPWD.length >= 6 && oldPWD.length <= 20) && (newPWD.length >= 6 && newPWD.length <= 20) && (surePWD.length >= 6 && surePWD.length <= 20)) {
            _sureBtn.userInteractionEnabled = YES;
            _sureBtn.backgroundColor = [UIColor orangeColor];
        }else{
            _sureBtn.userInteractionEnabled = NO;
            _sureBtn.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        }
    }else{
        if ((newPWD.length >= 6 && newPWD.length <= 20) && (surePWD.length >= 6 && surePWD.length <= 20)) {
            _sureBtn.userInteractionEnabled = YES;
            _sureBtn.backgroundColor = [UIColor orangeColor];
        }else{
            _sureBtn.userInteractionEnabled = NO;
            _sureBtn.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        }
    }
}

#pragma mark -- Action
- (void)surebuttonClick:(UIButton *)button{
    [_oldpasswordTF resignFirstResponder];
    [_newpasswordTF resignFirstResponder];
    [_surepasswordTF resignFirstResponder];
    NSString *oldPWD = _oldpasswordTF.text;
    NSString *newPWD = _newpasswordTF.text;
    NSString *surePWD = _surepasswordTF.text;
    //规则校验
//    if (self.isExistPWD && ![oldPWD checkPassword]) {
    if (self.isExistPWD && oldPWD.length<6) {
        SVPHUDError(@"请输入大于等于6位长度密码");
        return;
    }
    
//    if (![newPWD checkPassword]) {
    if (newPWD.length<6) {
        SVPHUDError(@"请输入大于等于6位长度密码");
        return;
    }
    
    if ([newPWD isEqualToString:oldPWD]) {
        SVPHUDError(@"新密码与旧密码一致，重新输入");
        return;
    }
    
    if (![surePWD isEqualToString:newPWD]) {
        SVPHUDError(@"确认密码和新密码不一致");
        return;
    }
    
//    if (![surePWD checkPassword]) {
//        //不是6-20位数字和字母组合
//        SVPHUDError(NetErrorAlertText);
//        return;
//    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    dic[@"old_password"] =  [oldPWD aci_encryptWithAES];
    dic[@"new_password"] = [newPWD aci_encryptWithAES];
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Setting_editPassword_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            //绑定成功
            [DataToolsControl deallocUserData];
            SVPHUDSuccess(responseObject[@"msg"]);
            
            [weakSelf performSelector:@selector(signoutClick) withObject:nil afterDelay:0.5];
            
//            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)signoutClick {
    [SharedApp showViewController:TabBarTypeOfSignin];
}

@end
